package com.boylegu.springboot_vue.controller;

import com.boylegu.springboot_vue.controller.pagination.PaginationFormatting;
import com.boylegu.springboot_vue.controller.pagination.PaginationMultiTypeValuesHelper;
import com.boylegu.springboot_vue.entities.Teachers;
import com.boylegu.springboot_vue.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("/api/teachers")
public class TeachersMainController {

    @Autowired
    private TeacherService teacherService;

    @Value(("${com.boylegu.paginatio.max-per-page}"))
    Integer maxPerPage;

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, PaginationMultiTypeValuesHelper> getStudentsAll(
            @RequestParam(value = "page", required = false) Integer pages
    ) {
        if (pages == null) {
            pages = 1;
        }
        Sort sort = new Sort(Direction.ASC, "id");

        Pageable pageable = new PageRequest(pages - 1, maxPerPage, sort);

        PaginationFormatting paginInstance = new PaginationFormatting();

        return paginInstance.filterQuery(pageable, PaginationFormatting.TYPE_TEACHER);
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Teachers> getDetail(@PathVariable String id) {

        Teachers student = new Teachers();
        if (id != null && !"new".equalsIgnoreCase(id)) {
            student = teacherService.findById(id);
        }

        return new ResponseEntity<>(student, HttpStatus.OK);
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Teachers update(@PathVariable String id, @RequestBody Teachers data) {

        Teachers teachers = teacherService.findById(id);

        teachers.setAuthor(data.getAuthor());
        teachers.setTeacher_id(data.getTeacher_id());
        teachers.setTeacher_name(data.getTeacher_name());

        return teacherService.save(teachers);
    }

    @RequestMapping(value = "/create/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Teachers create(@RequestBody Teachers data) {

        Teachers result = null;

        // TODO validate data
        try {
            result = teacherService.save(data);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpStatus delete(@PathVariable String id) {

        teacherService.deleteById(id);

        return HttpStatus.OK;
    }

}